package com.tmall.tmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tmall.tmall.mapper.*;
import com.tmall.tmall.pojo.*;
import com.tmall.tmall.service.ProductService;
import com.tmall.tmall.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductImageMapper productImageMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private OrderitemMapper orderitemMapper;
    @Autowired
    private ProductCollectMapper productCollectMapper;

    @Override
    @Transactional
    public void insert(ProductGt productGt) {
        //新增用户
        Product product = new Product();
        product.setCid(productGt.getProductTypeId());
        product.setPrice(productGt.getPrice());
        product.setName(productGt.getName());
        product.setStock(productGt.getStock());
        product.setSubTitle(productGt.getSubTitle());
        product.setCreateDate(LocalDateTime.now());
        productMapper.insert(product);
        Integer id=product.getId();
        //图片
        ProductImage productImage = new ProductImage();
        productImage.setName(productGt.getMainImg());
        productImage.setPid(id);
        productImage.setType("mainImg");
        productImageMapper.insert(productImage);
        String[] imgs = productGt.getImgList().split("，");
        for (String img : imgs) {
            productImage.setName(img);
            productImage.setType("img");
            productImageMapper.insert(productImage);
        }
    }
    @Override
    public PageRt page(Integer pageNum, Integer pageSize, String name, Integer pid,Integer qwe) {
        PageHelper.startPage(pageNum, pageSize);
        List<ProductRt> list = productMapper.page(name,pid,qwe);
        for (ProductRt productRt : list) {
            String cName = categoryMapper.getById(productRt.getCid()).getName();
            productRt.setProductTypeName(cName);
            List<String> imgs = productImageMapper.getByPid(productRt.getId());
            String join = String.join("，", imgs);
            productRt.setImgList(join);
        }
        PageInfo<ProductRt> pageInfo = new PageInfo<>(list);
        return new PageRt(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    @Transactional
    public void deleteByIds(List<Integer> ids) {
        for (Integer id : ids) {
            Product product = Product.builder().id(id).deleteAt(LocalDateTime.now()).build();
            productMapper.update(product);
            productImageMapper.deleteByPid(id,LocalDateTime.now());
        }
    }

    @Override
    public ProductRt getById(Integer id) {
        ProductRt productRt = productMapper.getById(id);
        List<String> imgs = productImageMapper.getByPid(id);
        String join = String.join("，", imgs);
        productRt.setImgList(join);
        Category category = categoryMapper.getById(productRt.getCid());
        productRt.setProductTypeName(category.getName());
        //看看当前用户是有添加当前物品到收藏
        Integer pcid = productCollectMapper.getPU(id, ThreadLocalUtils.getUid());
        if(pcid!=null)productRt.setCollect(pcid);
        return productRt;
    }

    @Override
    @Transactional
    public void update(ProductGt productGt) {
        //删除图片
        productImageMapper.deleteByPid(productGt.getId(), LocalDateTime.now());
        //修改用户
        Product product = new Product();
        product.setId(productGt.getId());
        product.setCid(productGt.getProductTypeId());
        product.setPrice(productGt.getPrice());
        product.setName(productGt.getName());
        product.setStock(productGt.getStock());
        product.setSubTitle(productGt.getSubTitle());
        product.setCreateDate(LocalDateTime.now());
        productMapper.update(product);
        //图片
        ProductImage productImage = new ProductImage();
        productImage.setName(productGt.getMainImg());
        productImage.setPid(productGt.getId());
        productImage.setType("mainImg");
        productImageMapper.insert(productImage);
        String[] imgs = productGt.getImgList().split("，");
        for (String img : imgs) {
            productImage.setName(img);
            productImage.setType("img");
            productImageMapper.insert(productImage);
        }
    }

    @Override
    public List<ProductRt> selectAll() {
        List<ProductRt> list = productMapper.selectAll();
        for (ProductRt productRt : list) {
            List<String> imgs = productImageMapper.getByPid(productRt.getId());
            String join = String.join("，", imgs);
            productRt.setImgList(join);
        }
        return list;
    }

    @Override
    public List<ProductRt> getByCid(Integer typeId) {
        List<ProductRt> list = productMapper.page(null,typeId,1);
        for (ProductRt productRt : list) {
            String cName = categoryMapper.getById(productRt.getCid()).getName();
            productRt.setProductTypeName(cName);
            List<String> imgs = productImageMapper.getByPid(productRt.getId());
            String join = String.join("，", imgs);
            productRt.setImgList(join);
        }
        return list;
    }
}
